#include "Common.h"
#include "AudioFile.h"
#include "AudioStream.h"

namespace Audio
{
	using namespace IO;	

	void SourceFileNode::GetData(UINT8* buffer, UINT32 size, UINT32& read)
	{
		read = min(size, file->DataSize() - pos);
		read = file->Read(buffer, read);
		pos += read;
	}
	BOOL SourceFileNode::End() const
	{
		return pos >= file->DataSize();
	}
	void SourceFileNode::SetParameter(UINT32 id, void* value)
	{

	}
	void SourceFileNode::Rewind()
	{
		pos = 0;
		file->Seek(file->DataPosition());		
	}
	

	void BufferNode::GetData(UINT8* buffer, UINT32 bufferSize, UINT32& read)
	{
		read = min(bufferSize, size - pos);
		std::copy(data + pos, data + pos + read, buffer);
		pos += read;
	}
	BOOL BufferNode::End() const
	{
		return pos >= size;
	}
	void BufferNode::SetParameter(UINT32 id, void* value)
	{

	}
	void BufferNode::Rewind()
	{
		pos = 0;
	}



	AudioStream* AudioStream::Create(const AudioStreamCreateParameter& p)
	{
		AudioStream* stream = new AudioStream();
		stream->loop = p.Loop;
		if(p.File != NULL)
		{
			stream->root = new SourceFileNode(p.File);
		}		
		return stream;
	}

	void AudioStream::Destroy(AudioStream* as)
	{
		delete as;
	}

	
}
